"use strict";

/** 表示一个矩形 */
class Rectangle {
    /**
     * 创建矩形
     * @param x 左上角X坐标
     * @param y 左上角Y坐标
     * @param width 矩形宽度
     * @param heigt 矩形高度
     */
    constructor(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        this._rotate = 0;
        this._realVertex = null;
    }

    /** 获取矩形的中心坐标 */
    get center() {
        return {
            x: this.x + this.width / 2,
            y: this.y + this.height / 2
        };
    }

    get rotate() {
        return this._rotate;
    }

    set rotate(value) {
        this._rotate = value;

        //在设置了旋转后重新计算矩形的真实坐标
        this._realVertex = value === 0 ? null : this.getVertexVector(this.center).map(vector => common.vectorRotate(vector, value, this.center));
    }

    /** 获取矩形旋转后的真实坐标 */
    getRealVertex() {
        return this._realVertex || this.getVertexVector();
    }

    /** 获取矩形的四个顶点相对于给定坐标的向量 */
    getVertexVector({x, y} = { x: 0, y: 0 }) {
        return [
            //左上角
            { x: this.x - x, y: this.y - y },
            //右上角
            { x: this.x + this.width - x, y: this.y - y },
            //右下角
            { x: this.x + this.width - x, y: this.y + this.height - y },
            //左下角
            { x: this.x - x, y: this.y + this.height - y }
        ]
    }
}